﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Web;
using PetaPoco;
using GsnHelper;
using ZhongheCms.Controllers;

namespace ZhongheCms.Models
{
    public partial class GswTblJobInfo
    {
        [ResultColumn]
        public List<GswTblBaseInfo> BaseInfos { get; set; }
        [ResultColumn]
        public List<GswViewJobWork> JobWorks { get; set; }
        [ResultColumn]
        public List<ProMasterType> ProTypes { get; set; }
        [ResultColumn]
        public List<GswTblJobInfo>  JobInfos { get; set; }
        [ResultColumn]
        public List<GswTblContent> Notices { get; set; }
        [ResultColumn]
        public List<GswTblContent> JobNews { get; set; }
        [ResultColumn]
        public List<GswViewJobWorkAll> JobWorkAlls { get; set; }

        public static Page<GswTblJobInfo> GetJobInfo(string sdate, string edate, string jname,int cusid,int sflag=0,int pageIndex=1)
        {
            //var sql = "select * from GswViewJobInfo where SourceFlag="+sflag+" and ParentId="+cusid;
            var sql = "select * from GswViewJobInfo where ParentId=" + cusid;
            int flag = 0;
            if (InputValid.IsDate(sdate))
            {
                flag = 1;
                sql += " and PubDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                flag = 1;
                sql += " and PubDate<='" + edate + "'";
            }
            if (!string.IsNullOrEmpty(jname))
            {
                flag = 1;
                sql += " and JobTitle like '%" + jname + "%'";
            }
            if (flag == 0)
                sql += " and JobState<2";
            sql += " order by PubDate desc";
            var db = new ZhongheRepo();
            
            var jobInfo = db.Page<GswTblJobInfo>(pageIndex,20,sql);
            return jobInfo;

        }

        public static Page<GswTblJobInfo> GetJobInfoAudit(string sdate, string edate, string jname, int cusid,int pageIndex)
        {
            var sql = "select * from GswViewJobInfo where ParentId=" + cusid;
            int flag = 0;
            if (InputValid.IsDate(sdate))
            {
                flag = 1;
                sql += " and PubDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                flag = 1;
                sql += " and PubDate<='" + edate + "'";
            }
            if (!string.IsNullOrEmpty(jname))
            {
                flag = 1;
                sql += " and JobTitle like '%" + jname + "%'";
            }
            if (flag == 0)
                sql += " and JobState=0";
            sql += " order by PubDate desc";
            var db = new ZhongheRepo();
            var jobInfo = db.Page<GswTblJobInfo>(pageIndex,20,sql);
            return jobInfo;

        }

        public static Page<GswViewJobInfo> GetJobInfoWork(string sdate, string edate, string jname, int cusid,int pageIndex)
        {
           // var sql = "select * from GswViewJobInfo where SourceFlag=" + sflag + " and JobState>0 and ParentId=" + cusid;
            var sql = "select * from GswViewJobInfo where  JobState>0 and ParentId=" + cusid;
            int flag = 0;
            if (InputValid.IsDate(sdate))
            {
                flag = 1;
                sql += " and PubDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                flag = 1;
                sql += " and PubDate<='" + edate + "'";
            }
            if (!string.IsNullOrEmpty(jname))
            {
                flag = 1;
                sql += " and JobTitle like '%" + jname + "%'";
            }
            if (flag == 0)
                sql += " and JobState=1";
            sql += " order by PubDate desc";
            var db = new ZhongheRepo();
            var jobInfo = db.Page<GswViewJobInfo>(pageIndex,20,sql) ;
            return jobInfo;

        }

        public static Page<GswViewJobInfo> GetJobInfoPayNotice(string sdate, string edate, string jname, int cusid,int pageIndex)
        {
            //var sql = "select * from GswViewJobInfo where  SourceFlag=" + sflag + " and JobState>0 and ParentId=" + cusid;
            var sql = "select * from GswViewJobInfo where JobState>0 and ParentId=" + cusid;
            int flag = 0;
            if (InputValid.IsDate(sdate))
            {
                flag = 1;
                sql += " and PubDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                flag = 1;
                sql += " and PubDate<='" + edate + "'";
            }
            if (!string.IsNullOrEmpty(jname))
            {
                flag = 1;
                sql += " and JobTitle like '%" + jname + "%'";
            }
            if (flag == 0)
                sql += " and PayNoticeFlag=0";
            sql += " order by PubDate desc";
            var db = new ZhongheRepo();
            var jobInfo = db.Page<GswViewJobInfo>(pageIndex, 20, sql);
            return jobInfo;

        }
        public static List<ProMasterType> GetJobTypes(int cusid)
        {
            var fid = "59";

            var strSql = "select Id,MasterType from GswTblBaseInfo where MasterId=0 and FunctionId=" + fid + " and CustomerId=" + cusid + " order by TypeNo";
            var mType = ZhongheRepo.GetInstance().Fetch<ProMasterType>(strSql);
            //var subTypeExiste = GswTblBaseInfo.FirstOrDefault("where MasterId>0 and CustomerId=" + cusid + " and  FunctionId=" + 37);
            //if (subTypeExiste == null) return mType;
            foreach (var m in mType)
            {
                m.SubFlag = 1;
                strSql = "select Id,SubType from GswTblBaseInfo where MasterId=" + m.Id + " Order By TypeNo";
                m.ProSubTypes = ZhongheRepo.GetInstance().Fetch<ProSubType>(strSql);
            }

            return mType;

        }
    }

    public class JobinfoPageModel
    {
        public List<GswTblBaseInfo> BaseInfos { get; set; }
        public Page<GswTblJobInfo> JobInfos { get; set; }
    }
}